﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineMealOrdering.Domain.Base
{
    public abstract class EntityBase
    {
        /// <summary>
        /// Entity Id (the default key)
        /// </summary>
        public virtual int Id { get; set; }

        /// <summary>
        /// Assert if one entity object is equal with another,
        /// return true when object hash code or Id is equal with another's 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if ((obj as EntityBase) == null)
                return false;

            if (this.Id == 0 && (obj as EntityBase).Id == 0)
                return Object.ReferenceEquals(this, obj);

            return this.Id == ((EntityBase)obj).Id;
        }

        /// <summary>
        /// if Id !=0 return Id, otherwise return raw hash code
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            if (this.Id == 0)
                return base.GetHashCode();

            return this.Id;
        }
    }
}
